ids=[-1:-1:-6 1:7];
[velP,velM,velP_ind,velM_ind,velP_raw,velM_raw]=get_vel_ind_from_adata(aux_data);
ips_arr={};
for und=1:length(ids)
    psonsets=find(diff((round(2*aux_data(3,:)))==ids(und))==1);
    tmp_ons=[];
    for gnd=1:length(psonsets)
        cur_win=psonsets(gnd)-500:psonsets(gnd)+200;
        if sum(velM_ind(cur_win))/length(cur_win)==1
            tmp_ons(gnd)=psonsets(gnd);
        end
    end
    tmp_ons(tmp_ons==0)=[];
    fb1=[frame_times(1) frame_times(20000)];
    fb2=[frame_times(61001) frame_times(81000)];
    psonsets=[tmp_ons(tmp_ons>fb1(1)&tmp_ons<fb1(2)) tmp_ons(tmp_ons>fb2(1)&tmp_ons<fb2(2))];
    ipsonsets=[];
    for ind=1:length(psonsets)
        [~,tmp]=min(abs(iframe_times-psonsets(ind)));
        ipsonsets(ind)=tmp;
    end
    throw_away=[];
    for vnd=1:length(nbr_iframes)-1
        throw_away=sum(nbr_iframes(1:vnd));
        ipsonsets(ismember(ipsonsets,throw_away-10:throw_away+10))=[];
    end
    ips_arr{und}=ipsonsets;
end

irun=[];
irun_tmp=[find(iframe_times>fb1(1) & iframe_times<fb1(2)) find(iframe_times>fb2(1) & iframe_times<fb2(2))];
irun=irun_tmp(velM_ind(iframe_times(irun_tmp))==1);

ppinds1=[0 16 28];
ppinds2=[3 18 30];
% ppinds1=[16];
% ppinds2=[-14];

ord=[1 2 3 8 9 10 4 5 6 11 12 13 7];
cols={'r' 'y' 'b' 'g'};
figure;
for ond=1:13
    subplot(2,7,ord(ond))
    hold on
    plot(pupil_pos(1,irun),pupil_pos(2,irun),'k.')
    for hnd=1:3
        for gnd=1:length(ips_arr{ond})
            plot(pupil_pos(1,ips_arr{ond}(gnd)+ppinds1(hnd):ips_arr{ond}(gnd)+ppinds2(hnd)),pupil_pos(2,ips_arr{ond}(gnd)+ppinds1(hnd):ips_arr{ond}(gnd)+ppinds2(hnd)),[cols{hnd} '.'])
%             plot(pupil_pos(1,ips_arr{ond}(gnd)+ppinds1(hnd)),pupil_pos(2,ips_arr{ond}(gnd)+ppinds1(hnd)),[cols{hnd} '.'])
            
        end
    end
end

ord1=[3 6];
ord2=[7 10];
figure;
hold on
plot(pupil_pos(1,irun),pupil_pos(2,irun),'k.')
for ond=1:2
    for gnd=1:length(ips_arr{ord1(ond)})
        plot(pupil_pos(1,ips_arr{ord1(ond)}(gnd)+ppinds1(hnd)),pupil_pos(2,ips_arr{ord1(ond)}(gnd)+ppinds1(hnd)),'r.')
    end
end
hold on
for ond=1:2
    for gnd=1:length(ips_arr{ord2(ond)})
        plot(pupil_pos(1,ips_arr{ord2(ond)}(gnd)+ppinds1(hnd)),pupil_pos(2,ips_arr{ord2(ond)}(gnd)+ppinds1(hnd)),'g.')
    end
end




% ppinds1=[0 16 28];
% ppinds2=[3 18 30];
% ord=[1 2 3 8 9 10 4 5 6 11 12 13 7];
% cols={'y' 'r' 'b'};
% figure;
% for ond=1:13
%     subplot(2,7,ord(ond))
%     hold on
%     plot(pupil_pos(1,[find(iframe_times>fb1(1) & iframe_times<fb1(2)) find(iframe_times>fb2(1) & iframe_times<fb2(2))]),pupil_pos(2,[find(iframe_times>fb1(1) & iframe_times<fb1(2)) find(iframe_times>fb2(1) & iframe_times<fb2(2))]),'k.')
%     for hnd=1:3
%         for gnd=1:length(ips_arr{ond})
%             plot(pupil_pos(1,ips_arr{ond}(gnd)+ppinds1(hnd):ips_arr{ond}(gnd)+ppinds2(hnd)),pupil_pos(2,ips_arr{ond}(gnd)+ppinds1(hnd):ips_arr{ond}(gnd)+ppinds2(hnd)),[cols{hnd} '.'])
%         end
%     end
% end



ids=[-1:-1:-6 1:7];
[velP,velM,velP_ind,velM_ind,velP_raw,velM_raw]=get_vel_ind_from_adata(aux_data);
ips_arr={};
for und=1:length(ids)
    psonsets=find(diff((round(2*aux_data(3,:)))==ids(und))==1);
    tmp_ons=[];
    for gnd=1:length(psonsets)
        cur_win=psonsets(gnd)-500:psonsets(gnd)+1000;
        if sum(velM_ind(cur_win))==0
            if sum(velP_ind(cur_win))/length(cur_win)==1
                tmp_ons(gnd)=psonsets(gnd);
            end
        end
    end
    tmp_ons(tmp_ons==0)=[];
    pb1=[min(frame_times(20001:40500)) max(frame_times(20001:40500))];
    pb2=[min(frame_times(40501:61000)) max(frame_times(40501:61000))];
    pb3=[min(frame_times(81001:101500)) max(frame_times(81001:101500))];
    pb4=[min(frame_times(101501:122000)) max(frame_times(101501:122000))];
    psonsets=[tmp_ons(tmp_ons>pb1(1)&tmp_ons<pb1(2)) tmp_ons(tmp_ons>pb2(1)&tmp_ons<pb2(2)) tmp_ons(tmp_ons>pb3(1)&tmp_ons<pb3(2)) tmp_ons(tmp_ons>pb4(1)&tmp_ons<pb4(2))];
%     psonsets=[tmp_ons(tmp_ons>pb1(1)&tmp_ons<pb1(2)) tmp_ons(tmp_ons>pb2(1)&tmp_ons<pb2(2))];
%     psonsets=[tmp_ons(tmp_ons>pb3(1)&tmp_ons<pb3(2)) tmp_ons(tmp_ons>pb4(1)&tmp_ons<pb4(2))];
    ipsonsets=[];
    for ind=1:length(psonsets)
        [~,tmp]=min(abs(iframe_times-psonsets(ind)));
        ipsonsets(ind)=tmp;
    end
    throw_away=[];
    for vnd=1:length(nbr_iframes)-1
        throw_away=sum(nbr_iframes(1:vnd));
        ipsonsets(ismember(ipsonsets,throw_away-10:throw_away+10))=[];
    end
    ips_arr{und}=ipsonsets;
end
ord=[1 2 3 8 9 10 4 5 6 11 12 13 7];
cols={'y' 'r' 'b'};
figure;
for ond=1:13
    subplot(2,7,ord(ond))
    hold on
    plot(pupil_pos(1,:),pupil_pos(2,:),'k.')
    for hnd=1:3
        for gnd=1:length(ips_arr{ond})
            plot(pupil_pos(1,ips_arr{ond}(gnd)+(3*(hnd-1)):ips_arr{ond}(gnd)+(3*hnd)),pupil_pos(2,ips_arr{ond}(gnd)+(3*(hnd-1)):ips_arr{ond}(gnd)+(3*hnd)),[cols{hnd} '.'])
        end
    end
end